package com.sjc.wallet.DService;

import com.sjc.wallet.entity.VirtualWalletEntity;
import com.sjc.wallet.exception.InsufficientBalanceException;
import com.sjc.wallet.exception.InvalidAmountException;
import com.sjc.wallet.repository.VirtualWalletRepository;
import com.sjc.wallet.repository.VirtualWalletTransactionRepository;

import java.math.BigDecimal;

/**
 * @Classname DVirtualWalletService
 * @Description
 * @Date 2020/5/2 15:47
 * @Created by SangJiacun
 */
public class DVirtualWalletService {
    private VirtualWalletRepository walletRepo;
    private VirtualWalletTransactionRepository transactionRepo;

    /**
     * 获取虚拟钱包
     * @param walletId
     * @return
     */
    public VirtualWallet getVirtualWallet(Long walletId) {
        VirtualWalletEntity walletEntity = walletRepo.getWalletEntity(walletId);
        VirtualWallet wallet = convert(walletEntity);
        return wallet;
    }

    public BigDecimal getBalance(Long walletId) {
        return walletRepo.getBalance(walletId);
    }

    public void debit(Long walletId, BigDecimal amount) throws InsufficientBalanceException {
        VirtualWalletEntity walletEntity = walletRepo.getWalletEntity(walletId);
        VirtualWallet wallet = convert(walletEntity);
        wallet.debit(amount);
        walletRepo.updateBalance(walletId, wallet.balance());
    }

    public void credit(Long walletId, BigDecimal amount) throws InvalidAmountException {
        VirtualWalletEntity walletEntity = walletRepo.getWalletEntity(walletId);
        VirtualWallet wallet = convert(walletEntity);
        wallet.credit(amount);
        walletRepo.updateBalance(walletId, wallet.balance());
    }

    private VirtualWallet convert(VirtualWalletEntity walletEntity) {
        return null;
    }

    public void transfer(Long fromWalletId, Long toWalletId, BigDecimal amount) {
        //...跟基于贫血模型的传统开发模式的代码一样...

    }
}
